package haois.constant;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by haol on 2017/4/25.
 */
public class Reply<T> {
    private String code; // 0 返回码
    private String msg;//具体信息
    private T data;//返回需要的数据
    private String dataType;

    public Reply(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Reply(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        if (data != null){
            this.dataType = data.getClass().getName();
        }

    }

    public Reply(String code, String msg, T data, String dataType) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.dataType = dataType;
    }

    public String toJsonNotNull() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }

    public String toJson() {
        Gson gson = new GsonBuilder().serializeNulls().create();
        return gson.toJson(this).replaceAll("null", "\"\"");
    }

    public static String createReply(String code, String msg) {
        return new Reply(code, msg).toJson();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }


    public Reply dataIsEmpty() {
        Object object = this.data;
        if (object == null) {
            this.msg = ReturnState.MSG_02;
            this.code = ReturnState.CODE_02;
        }else {
            if (object instanceof List) {
                List list = (List) object;
                if (list.size() == 0) {
                    this.msg = ReturnState.MSG_01;
                    this.code = ReturnState.CODE_01;
                }
            }
        }
        return this;
    }

    /**
     * @api {GET} /api  生成返回数据
     * @apiGroup apiReply
     * @apiName getApiMsg
     * @apiDescription 请求返回数据 0XX为正常返回，1XX为异常返回，未知错误为服务抛出未知异常
     * @apiSuccess (200) {String} msg 信息
     * @apiSuccess (200) {int} code  0xx:正常返回/1xx:异常返回
     * @apiSuccess (200) {T} data 获取的真实数据
     * @apiSuccessExample {json} 返回样例:
     * {"code":"00","msg":"NODE ONE","data":[{"code":"00","msg":"正常响应"},{"code":"01","msg":"查询为空"},{"code":"02","msg":"没有对应数据"},{"code":"03","msg":"违反唯一性"},{"code":"04","msg":"没有权限"},{"code":"05","msg":"缺省值"},{"code":"10","msg":"普通错误"},{"code":"11","msg":"系统内部错误"},{"code":"12","msg":"参数传递不正确"},{"code":"13","msg":"非法请求"},{"code":"15","msg":"缺省值"}]}
     */
    public static List<Reply> getApiMsg() {

        List<Reply> replies = new ArrayList<Reply>();
        Reply reply00 = new Reply(ReturnState.CODE_00, ReturnState.MSG_00);
        Reply reply01 = new Reply(ReturnState.CODE_01, ReturnState.MSG_01);
        Reply reply02 = new Reply(ReturnState.CODE_02, ReturnState.MSG_02);
        Reply reply03 = new Reply(ReturnState.CODE_03, ReturnState.MSG_03);
        Reply reply04 = new Reply(ReturnState.CODE_04, ReturnState.MSG_04);
        Reply reply05 = new Reply(ReturnState.CODE_05, ReturnState.MSG_05);

        Reply reply10 = new Reply(ReturnState.CODE_10, ReturnState.MSG_10);
        Reply reply11 = new Reply(ReturnState.CODE_11, ReturnState.MSG_11);
        Reply reply12 = new Reply(ReturnState.CODE_12, ReturnState.MSG_12);
        Reply reply13 = new Reply(ReturnState.CODE_13, ReturnState.MSG_13);
        Reply reply15 = new Reply(ReturnState.CODE_15, ReturnState.MSG_15);

        replies.add(reply00);
        replies.add(reply01);
        replies.add(reply02);
        replies.add(reply03);
        replies.add(reply04);
        replies.add(reply05);

        replies.add(reply10);
        replies.add(reply11);
        replies.add(reply12);
        replies.add(reply13);
        replies.add(reply15);
        return replies;
    }
}
